/*
 * Project: daycare-ent
 * Package: com.made2ordersoftware.daycare.domain
 * File: PersonEntity
 * Created on: Sep 15, 2013
 */
/**
 * Copyright (c) 2010 Made 2 Order Software, LLC
 *
 * All rights reserved. No part of this software may be reproduced, stored in a retrieval system, or
 * transmitted in any form or by any means, without the prior written permission of Made 2 Order Software.
 *
 * You may not distribute the URL or redistribute the files to third parties. You may transfer the files onto
 * a single storage device so long as you can prove ownership of the software. You may not reverse engineer,
 * decompile, or disassemble the software or media files. You may not modify or translate the software or
 * media, or distribute copies of the software or media without the written consent of Made 2 Order Software,
 * LLC.
 */
package com.made2ordersoftware.daycare.domain;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Objects;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Entity class representing the a child registered with the day care.
 *
 * @author David Canning <dcannin@gmail.com>
 * @version 1.0.0
 */
@Entity
@DiscriminatorValue(value = "CHILD")
@NamedQuery(name = "Child.findChildIDs", query = "SELECT c.id FROM ChildEntity c")
@XmlRootElement
public class ChildEntity extends PersonEntity {

    private static final Logger LOG = LoggerFactory.getLogger(ChildEntity.class);
    private static final long serialVersionUID = 1L;
    @OneToOne(cascade = CascadeType.ALL, mappedBy = "child")
    private ChildProfileEntity childProfile;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "child")
    private Collection<TimeWatchedEntity> timeWatched;
    @ManyToMany(mappedBy = "children")
    private Collection<ParentEntity> parents;

    /**
     * Creates an instance of <code>ChildEntity</code>.
     */
    public ChildEntity() {
    }

    /**
     * Creates an instance of <code></code>.
     *
     * @param firstName First name of child
     * @param lastName  Last name of child
     */
    public ChildEntity(String firstName, String lastName) {
        super(firstName, lastName);
    }

    /**
     * Returns the child's profile information.
     *
     * @return The child's profile information
     */
    public ChildProfileEntity getChildProfile() {
        return childProfile;
    }

    /**
     * Sets the child's profile information.
     *
     * @param childProfile The ChildProfile to set for this child
     */
    public void setChildProfile(ChildProfileEntity childProfile) {
        this.childProfile = childProfile;
    }

    /**
     * Returns the list of times this child was signed into the day care.
     *
     * @return The Collection of TimeWatchedEntity objects for this child
     */
    @XmlTransient
    public Collection<TimeWatchedEntity> getTimeWatched() {
        return Collections.unmodifiableCollection(timeWatched);
    }

    /**
     * Sets the list of times this child was signed into the day care.
     *
     * @param timeWatched The Collection of TimeWatchedEntity objects for this child
     */
    public void setTimeWatched(Collection<TimeWatchedEntity> timeWatched) {
        this.timeWatched = timeWatched;
    }

    /**
     * Adds a time record to the list of times this child was signed into the day care.
     *
     * @param time The date and time this child was signed into the day care
     */
    public void addTimeWatched(TimeWatchedEntity time) {
        if (timeWatched == null) {
            timeWatched = new ArrayList<TimeWatchedEntity>();
        }
        timeWatched.add(time);
    }

    /**
     * Returns the list of parents associated with this child.
     *
     * @return The Collection of ParentEntity object for this child
     */
    @XmlTransient
    public Collection<ParentEntity> getParents() {
        return Collections.unmodifiableCollection(parents);
    }

    /**
     * Sets the list of parents associated with this child.
     *
     * @param parents The Collection of ParentEntity objects for this child
     */
    public void setParents(Collection<ParentEntity> parents) {
        this.parents = parents;
    }

    /**
     * Adds a parent to this child's parent list (parents).
     *
     * @param parent The ParentEntity to add to the list
     */
    public void addParent(ParentEntity parent) {
        if (parents == null) {
            parents = new ArrayList<ParentEntity>();
        }
        parents.add(parent);
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 59 * hash + Objects.hashCode(this.getId());
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof ChildEntity)) {
            return false;
        }
        ChildEntity other = (ChildEntity) object;
        return Objects.equals(this.getId(), other.getId());
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder(super.toString());
        sb.append(this.childProfile.toString());
        sb.append("}");
        return sb.toString();
    }

}
